const mysql = require("mysql");

const obj = {
  host: "localhost",
  user: "root",
  password: "root",
  database: "chunyuyisheng",
};

if (process.env.npm_lifecycle_event !== 'dev') {
  obj.user = 'root123'
  obj.password = 'root123'
}
const pool = mysql.createPool(obj);

// 连接
const exec = (sql, values) => {
  return new Promise((resolve, reject) => {
    pool.getConnection((err, conn) => {
      if (err) {
        //连接错误
        reject(err);
      } else {
        //连接成功
        conn.query(sql, values, (err, data) => {
          if (err) {
            //操作失败
            reject(err);
          } else {
            resolve(data);
          }
        });
      }
      // 当连接不再使用时,归还到连接池
      conn.release();
    });
  });
};

module.exports = {
  pool,
  exec,
};
